<?php

declare(strict_types=1);

namespace App\Model;


/**
 * @property int $id
 * @property int $uid
 * @property int $sex 性别
 * @property int $total_godvalue 男神值或女神值
 * @property int $total_popularity 人气值
 */
class UserScore extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'tb_user_score';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = [];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'uid' => 'integer', 'sex' => 'integer', 'total_godvalue' => 'integer', 'total_popularity' => 'integer'];

    /**
     * 插入或更新用户的各项得分值数据
     * @param int $uid 男性或女性用户id
     * @param int $sex 性别，1男，2女
     * @param array $data total_godvalue,total_popularity键值对，可一对可多对
     * @return bool         返回
     */
    public function insertUpdateData($uid, $sex, $data = array())
    {
        UserScore::query()->updateOrInsert(['uid' => $uid, 'sex' => $sex], $data);
        // $values = "";
        // if (!is_array($data) || empty($data)) {
        //     return false;
        // }
        // $key = array('uid', 'sex'); //顺序不能乱
        // $bindVal = array($uid, $sex);
        // $bindVal2 = array();
        // $param = array('?', '?');
        // $setstr = "";
        // foreach ($data as $k => $v) {
        //     $key[] = "`" . $k . "`";
        //     $bindVal[] = $v;
        //     $bindVal2[] = $v;
        //     $param[] = "?";
        //     $setstr .= "`" . $k . "` = `" . $k . "` + ?,";
        // }
        // $kstr = implode(',', $key);
        // $vstr = implode(',', $param);
        // $bindVal = array_merge($bindVal, $bindVal2);
        // $setstr = rtrim($setstr, ",");
        // try {
        //     $sql = "insert into {$this->table} ($kstr) values ($vstr) on duplicate key update $setstr";
        //     $pd = $this->createCommand($sql);
        //     $pd->execute($bindVal);
        // } catch (PDOException $e) {
        //     error_log('Connection failed: ' . $e->getMessage());
        //     Tool::JSON(\CodeData::NO_PROMPT, '-1', 'database error');
        // }
        return true;
    }


}
